package com.wardrobe.soa.backend.operator;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@RequestMapping("/operator")
public class AdminController {


    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Resource
    private com.wardrobe.soa.backend.operator.AdminOperatorService adminOperatorService;

    @RequestMapping("/list")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> getAdminList() {
        return adminOperatorService.getOperatorList();

    }

    @RequestMapping("/delete")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> delAdmin(AdminOperatorBean fromBean) {
        Map<String, Object> retMap = Maps.newHashMap();

        if (fromBean.getId() <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "删除失败");
            return retMap;
        }
        final int retCode = adminOperatorService.delOperator(fromBean.getId());
        if (retCode == 1) {
            retMap.put("retCode", 1);
            retMap.put("retMsg", "删除成功");
            return retMap;
        }
        retMap.put("retCode", -1);
        retMap.put("retMsg", "删除失败");
        return retMap;

    }

    @RequestMapping("/querById")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> getOperatorById(AdminOperatorBean fromBean) {
        final AdminOperatorBean adminOperatorBean = adminOperatorService.queryById(fromBean);
        Map<String, Object> retMap = Maps.newHashMap();
        if (adminOperatorBean == null) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "获取用户失败");
            return retMap;
        }
        retMap.put("retCode", 1);
        retMap.put("id", adminOperatorBean.getId());
        retMap.put("realName", adminOperatorBean.getRealName());
        retMap.put("nickName", adminOperatorBean.getNickName());
        retMap.put("description", adminOperatorBean.getDescription());
        return retMap;
    }


    @RequestMapping("/update")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> updateAdmin(AdminOperatorBean fromBean) {
        Map<String, Object> retMap = Maps.newHashMap();

        if (fromBean.getId() <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "修改失败");
            return retMap;
        }
        final int retCode = adminOperatorService.updateOperator(fromBean);
        if (retCode == 1) {
            retMap.put("retCode", 1);
            retMap.put("retMsg", "修改成功");
            return retMap;
        }
        retMap.put("retCode", -1);
        retMap.put("retMsg", "修改失败");
        return retMap;

    }

    @RequestMapping("/reset_pwd")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> resetPwd(AdminOperatorBean fromBean) {
        Map<String, Object> retMap = Maps.newHashMap();

        if (fromBean.getId() <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "重置失败");
            return retMap;
        }
        final int retCode = adminOperatorService.resetPwd(fromBean);
        if (retCode == 1) {
            retMap.put("retCode", 1);
            retMap.put("retMsg", "修改成功");
            return retMap;
        }
        retMap.put("retCode", -1);
        retMap.put("retMsg", "修改失败");
        return retMap;

    }

    @RequestMapping("/get_operator_name")
//    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> getOperatorName(AdminOperatorBean fromBean) {
        Map<String, Object> retMap = Maps.newHashMap();
        AdminOperatorBean adminOperatorBean = adminOperatorService.getCurrentUser();
        final String nickName = adminOperatorService.getCurrentUser().getNickName();
        if (StringUtils.isBlank(nickName)) {
            retMap.put("retCode", -1);
            retMap.put("nickName", "");
        } else {
            retMap.put("retCode", 1);
            retMap.put("nickName", nickName);
            retMap.put("realName",adminOperatorBean.getRealName());
        }
        return retMap;
    }

    @RequestMapping("/query_roles")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> getRoles() {
        Map<String, Object> retMap = Maps.newHashMap();

        final List<RoleBean> roleList = adminOperatorService.queryRoles();
        if (roleList != null && roleList.size() <= 0) {
            retMap.put("retCode", -1);
            retMap.put("list", "");
        } else {
            retMap.put("retCode", 1);
            retMap.put("list", roleList);
        }
        return retMap;
    }

    @RequestMapping("/query_roles_by_operatorId")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> getRolesByOperatorId(AdminOperatorBean bean) {
        Map<String, Object> retMap = Maps.newHashMap();

        final List<Map<String, Object>> roleList = adminOperatorService.getRoleByOperatorId(bean);
        if (roleList != null && roleList.size() <= 0) {
            retMap.put("retCode", -1);
            retMap.put("list", "");
        } else {
            retMap.put("retCode", 1);
            retMap.put("list", roleList);
        }
        return retMap;
    }

    @RequestMapping("/update_roles_by_operatorId")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> updateAdminOperatorRole(AdminOperatorBean bean) {
        Map<String, Object> retMap = Maps.newHashMap();

        adminOperatorService.removeRoleFromOperator(bean);
        int retNum = adminOperatorService.addRoleFromOperator(bean);
        if (retNum <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "");
        } else {
            retMap.put("retCode", 1);
            retMap.put("retMsg", "操作成功");
        }
        return retMap;
    }


    @RequestMapping("/query_permission")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> getPermissions() {
        Map<String, Object> retMap = Maps.newHashMap();

        final List<PermissionBean> permissionList = adminOperatorService.queryPermission();
        if (permissionList != null && permissionList.size() <= 0) {
            retMap.put("retCode", -1);
            retMap.put("list", "");
        } else {
            retMap.put("retCode", 1);
            retMap.put("list", permissionList);
        }
        return retMap;
    }

    @RequestMapping("/query_permission_by_id")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> getPermissionsByRoleId(int roleId) {
        Map<String, Object> retMap = Maps.newHashMap();

        final List<Map<String, Object>> permissionList = adminOperatorService.queryPermissionsByRoleId(roleId);
        if (permissionList != null && permissionList.size() <= 0) {
            retMap.put("retCode", -1);
            retMap.put("list", "");
        } else {
            retMap.put("retCode", 1);
            retMap.put("list", permissionList);
        }
        return retMap;
    }


    @RequestMapping("/add_role")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> addRole(RoleBean bean) {
        Map<String, Object> retMap = Maps.newHashMap();
        int operateFlag = 0;
        if (bean.getId() == 0) {
            operateFlag = adminOperatorService.addRole(bean);
        } else {
            operateFlag = adminOperatorService.updateRole(bean);
        }


        if (operateFlag <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "添加失败");
        } else {
            adminOperatorService.removePermissionFromRole(bean);
            adminOperatorService.addPermissionFromRole(bean);
            retMap.put("retCode", 1);
            retMap.put("retMsg", "添加成功");
        }
        return retMap;
    }

    @RequestMapping("/add_permission")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> addPermissions(PermissionBean bean) {
        Map<String, Object> retMap = Maps.newHashMap();
        int operateFlag = 0;
        if (bean.getId() == 0) {
            operateFlag = adminOperatorService.addPermission(bean);
        } else {
            operateFlag = adminOperatorService.updatePermissino(bean);
        }
        if (operateFlag <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "操作失败");
        } else {
            retMap.put("retCode", 1);
            retMap.put("retMsg", "操作成功");
        }
        return retMap;
    }

    @RequestMapping("/del_permisssion")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> delPermission(PermissionBean bean) {
        Map<String, Object> retMap = Maps.newHashMap();
        int insertFlag = 0;
        if (bean.getId() != 0) {
            insertFlag = adminOperatorService.delPermission(bean);
        }
        if (insertFlag <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "操作失败");
        } else {
            retMap.put("retCode", 1);
            retMap.put("retMsg", "操作成功");
        }
        return retMap;
    }

    @RequestMapping("/del_role")
    @RequiresPermissions(PermissionCode.ADMIN_OPERATOR_PERMISSION)
    public Map<String, Object> delRole(RoleBean bean) {
        Map<String, Object> retMap = Maps.newHashMap();
        int insertFlag = 0;
        if (bean.getId() != 0) {
            insertFlag = adminOperatorService.delRole(bean);
        }
        if (insertFlag <= 0) {
            retMap.put("retCode", -1);
            retMap.put("retMsg", "操作失败");
        } else {
            retMap.put("retCode", 1);
            retMap.put("retMsg", "操作成功");
        }
        return retMap;
    }

}